//
//  ReceiveGoodsController.m
//  Youhuiju
//
//  Created by liuovyue on 16/8/30.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "ReceiveGoodsController.h"
#import "CellForReceiveGoods.h"
#import "CellTableViewReceiveGoods.h"
#import "RefundViewController.h"
#import "ViewOrderViewController.h"


#import "GoodModel.h"
@interface ReceiveGoodsController ()<UICollectionViewDelegate, UICollectionViewDataSource,UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UILabel *labelTitle;
@property (nonatomic, strong)UIButton *buttonReturn;
@property (nonatomic, strong)UIView *viewBar;
@property (nonatomic, strong)UIButton *buttonSure;
@property (nonatomic, strong)UIView *viewBack;
@property (nonatomic, strong)UIButton* buttonFunction;
@property (nonatomic, strong)UIButton* buttonRemove;
@property (nonatomic, strong)UIView* viewFunction;
@property (nonatomic, strong)UICollectionView *collection;
@property (nonatomic, strong)NSArray *arr;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arrTableView;
@property (nonatomic, strong)CellTableViewReceiveGoods *cellT;
@property (nonatomic, strong)CellForReceiveGoods *cell;
@property (nonatomic, strong)UILabel *labelOne;
@property (nonatomic, strong)UILabel *labelTwo;
@property (nonatomic, strong)UILabel *labelThree;
@property (nonatomic, strong)UILabel *labelFour;
@property (nonatomic, strong)UILabel *labelFive;

@property(nonatomic,strong)GoodModel *model;

@end

@implementation ReceiveGoodsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createNavigationBar];
    self.arr = @[@"全部",@"待付款",@"待发货",@"待收货",@"待评价"];
    self.arrTableView = @[@"1"].mutableCopy;
    [self createCollection];
    self.view.backgroundColor = [UIColor colorWithHexString:@"efefef"];
    [self createTableView];
    
    
    
    
}

- (void)createNavigationBar{
    
    self.viewBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    self.viewBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewBar];
    
    
    
    
    
    
    self.labelTitle = [[UILabel alloc]init];
    self.labelTitle.text = @"我的订单";
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    self.labelTitle.textColor = [UIColor blackColor];
    self.labelTitle.font = [UIFont systemFontOfSize:18*FitW];
    [self.viewBar addSubview:self.labelTitle];
    
    [_labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //make.top.equalTo(self.viewBar.mas_top);
        make.left.equalTo(self.viewBar.mas_left).offset(100*FitW);
        make.right.equalTo(self.viewBar.mas_right).offset(-100*FitW);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-14*FitH);
        
        
        
    }];
    //返回按钮
    self.buttonReturn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonReturn setImage:[UIImage imageNamed:@"zfanhuijian"] forState:UIControlStateNormal];
    [self.buttonReturn addTarget:self action:@selector(tuihuiAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBar addSubview:self.buttonReturn];
    
    [_buttonReturn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.viewBar.mas_left).offset(17*FitW);
        make.width.mas_equalTo(22*FitW);
        make.height.mas_equalTo(22*FitH);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-13*FitH);
        
        
        
    }];
    
    self.buttonFunction = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonFunction setImage:[UIImage imageNamed:@"function"] forState:UIControlStateNormal];
    [self.buttonFunction addTarget:self action:@selector(functionAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBar addSubview:self.buttonFunction];
    
    [_buttonFunction mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.viewBar.mas_right).offset(-17*FitW);
        make.width.mas_equalTo(22*FitW);
        make.height.mas_equalTo(22*FitH);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-13*FitH);
        
        
        
    }];
    
  
    
}

#pragma mark - 功能按钮
- (void)functionAcion:(UIButton *)button {
    
    self.buttonRemove = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [self.buttonRemove addTarget:self action:@selector(removeAcion:) forControlEvents:UIControlEventTouchUpInside];
    self.buttonRemove.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    [self.view addSubview:self.buttonRemove];
    self.viewFunction = [[UIView alloc]init];
    self.viewFunction.backgroundColor = [UIColor whiteColor];
    [self.viewFunction.layer setMasksToBounds:YES];
    [self.viewFunction.layer setBorderWidth:1.0];
    self.viewFunction.layer.borderColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0].CGColor;
    
    [self.buttonRemove addSubview:self.viewFunction];
    [self.viewFunction mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(15*FitH);
        make.right.equalTo(self.view.mas_right).offset(-20*FitW);
        make.height.mas_equalTo(90*FitH);
        make.width.mas_equalTo(110*FitW);
    }];
    UIButton *buttonNews = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonNews setTitle:@"    消息" forState: UIControlStateNormal];
    buttonNews.titleLabel.font = [UIFont systemFontOfSize:18*FitW];
    buttonNews.tintColor = [UIColor blackColor];
    [buttonNews addTarget:self action:@selector(newsAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewFunction addSubview:buttonNews];
    [buttonNews setImage:[UIImage imageNamed:@"zxiaoxi"] forState:UIControlStateNormal];
    [buttonNews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewFunction.mas_top);
        make.right.equalTo(self.viewFunction.mas_right);
        make.left.equalTo(self.viewFunction.mas_left);
        make.height.mas_equalTo(45*FitH);
    }];
    UIButton *buttonFirst = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonFirst setTitle:@"    首页" forState: UIControlStateNormal];
    buttonFirst.titleLabel.font = [UIFont systemFontOfSize:18*FitW];
    buttonFirst.tintColor = [UIColor blackColor];
    [buttonFirst addTarget:self action:@selector(fitstAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewFunction addSubview:buttonFirst];
    [buttonFirst setImage:[UIImage imageNamed:@"shouye"] forState:UIControlStateNormal];
    [buttonFirst mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewFunction.mas_top).offset(45*FitH);
        make.right.equalTo(self.viewFunction.mas_right);
        make.left.equalTo(self.viewFunction.mas_left);
        make.height.mas_equalTo(45*FitH);
    }];
    
    
    
}
//点击去除功能栏
- (void)removeAcion:(UIButton *)button {
    [self.viewFunction removeFromSuperview];
    [self.buttonRemove removeFromSuperview];
}

- (void)newsAction:(UIButton *)button{
    NSLog(@"消息");
}
- (void)fitstAction:(UIButton *)button{
    NSLog(@"首页");
    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)tuihuiAcion:(UIButton *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

#pragma mark - collection
- (void)createCollection {
    
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    flow.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width / 5, 44*FitH);
    flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64*FitH, [UIScreen mainScreen].bounds.size.width, 44*FitH) collectionViewLayout:flow];
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.view addSubview:self.collection];
    
    [self.collection registerNib:[UINib nibWithNibName:@"CellForReceiveGoods" bundle:nil] forCellWithReuseIdentifier:@"CellForReceiveGoods"];
    
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
   return  self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellForReceiveGoods" forIndexPath:indexPath];
    self.cell.labelTitle.text = [self.arr objectAtIndex:indexPath.row];
    self.cell.labelTitle.tag = (indexPath.row + 1)*10;
//    if (indexPath.row == 0) {
//        self.cell.labelTitle.textColor = [UIColor colorWithHexString:@"#fd487f"];
//    }
    self.labelOne = [self.view viewWithTag:10];
    self.labelOne.textColor = [UIColor colorWithHexString:@"#fd487f"];
    self.cell.labelTitle.textAlignment = NSTextAlignmentCenter;
    return self.cell;
    
    
}

#pragma mark --collection 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.row) {
            case 0:
        {
            self.labelOne = [self.view viewWithTag:10];
            self.labelOne.textColor = [UIColor colorWithHexString:@"#fd487f"];
            self.labelTwo.textColor = [UIColor blackColor];
            self.labelThree.textColor = [UIColor blackColor];
            self.labelFour.textColor = [UIColor blackColor];
            self.labelFive.textColor = [UIColor blackColor];

            
            self.cellT.buttonEvaluation.alpha = 0;
            self.cellT.deleteOrder.alpha = 0;
            self.cellT.buttonContractWe.alpha =0;
            self.cellT.buttonSureGoods.alpha =1;
            self.cellT.cancelOrders.alpha = 0;
            self.cellT.buttonPayment.alpha =0;
            self.cellT.buttonLookExpress.alpha =1;
            self.cellT.buttonlookExpressTwo.alpha =0;
            
            
        }
            break;
        case 1:
        {
            self.labelTwo = [self.view viewWithTag:20];
            self.labelOne.textColor = [UIColor blackColor];
            self.labelTwo.textColor = [UIColor colorWithHexString:@"#fd487f"];
            self.labelThree.textColor = [UIColor blackColor];
            self.labelFour.textColor = [UIColor blackColor];
            self.labelFive.textColor = [UIColor blackColor];
            
            self.cellT.labelPayment.text = @"等待买家付款";
            self.cellT.buttonEvaluation.alpha = 0;
            self.cellT.deleteOrder.alpha = 0;
            self.cellT.buttonSureGoods.alpha =0;
            self.cellT.buttonContractWe.alpha =1;
            self.cellT.buttonLookExpress.alpha = 0;
            self.cellT.cancelOrders.alpha =1;
            self.cellT.buttonPayment.alpha =1;
            self.cellT.buttonlookExpressTwo.alpha =0;


        }
            break;
            case 2:
        {
         self.cellT.labelPayment.text = @"买家已付款";
            
            
            self.labelThree = [self.view viewWithTag:30];
            
            
            self.labelOne.textColor = [UIColor blackColor];
            self.labelThree.textColor = [UIColor colorWithHexString:@"#fd487f"];
            self.labelTwo.textColor = [UIColor blackColor];
            self.labelFour.textColor = [UIColor blackColor];
            self.labelFive.textColor = [UIColor blackColor];
            
            self.cellT.buttonEvaluation.alpha = 0;
            self.cellT.deleteOrder.alpha = 0;
            self.cellT.buttonSureGoods.alpha =0;
            self.cellT.buttonContractWe.alpha =0;
            self.cellT.buttonLookExpress.alpha = 0;
            self.cellT.buttonPayment.alpha =0;
            self.cellT.cancelOrders.alpha = 0;
            self.cellT.buttonlookExpressTwo.alpha =1;

            

        }
            break;
            case 3:
        {
            self.cellT.labelPayment.text = @"卖家已发货";
            self.labelFour = [self.view viewWithTag:40];
           
            self.labelOne.textColor = [UIColor blackColor];
            self.labelFour.textColor = [UIColor colorWithHexString:@"#fd487f"];
            self.labelThree.textColor = [UIColor blackColor];
            self.labelTwo.textColor = [UIColor blackColor];
            self.labelFive.textColor = [UIColor blackColor];
            
            self.cellT.buttonEvaluation.alpha = 0;
            self.cellT.deleteOrder.alpha = 0;
            self.cellT.buttonContractWe.alpha =0;
            self.cellT.buttonSureGoods.alpha =1;
            self.cellT.cancelOrders.alpha = 0;
            self.cellT.buttonPayment.alpha =0;
            self.cellT.buttonLookExpress.alpha =1;
            self.cellT.buttonlookExpressTwo.alpha =0;

        }
            break;
            case 4:
        {
            self.cellT.labelPayment.text = @"交易完成";
             self.labelFive = [self.view viewWithTag:50];
            self.labelOne.textColor = [UIColor blackColor];
            self.labelFive.textColor = [UIColor colorWithHexString:@"#fd487f"];
            self.labelThree.textColor = [UIColor blackColor];
            self.labelFour.textColor = [UIColor blackColor];
            self.labelTwo.textColor = [UIColor blackColor];
            
            self.cellT.buttonSureGoods.alpha =0;
            self.cellT.buttonContractWe.alpha =0;
            self.cellT.buttonEvaluation.alpha =1;
            self.cellT.cancelOrders.alpha = 0;
            self.cellT.buttonPayment.alpha =0;
            self.cellT.buttonLookExpress.alpha =1;
            self.cellT.deleteOrder.alpha = 1;
            self.cellT.buttonlookExpressTwo.alpha =0;


            
        }
            break;
        default:
            break;
    }
    
    
    
    
    
}



#pragma mark - tableView 
- (void)createTableView {
    self.tableView = [[UITableView alloc]init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        
        make.top.equalTo(self.collection.mas_bottom).offset(10*FitH);
        make.right.equalTo(self.view.mas_right);
        make.left.equalTo(self.view.mas_left);
        make.bottom.equalTo(self.view.mas_bottom);
        
        
    }];
    [self.tableView registerNib:[UINib nibWithNibName:@"CellTableViewReceiveGoods" bundle:nil]  forCellReuseIdentifier:@"CellTableViewReceiveGoods"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cellT = [tableView dequeueReusableCellWithIdentifier:@"CellTableViewReceiveGoods"];
   
    self.cellT.selectionStyle = UITableViewCellSelectionStyleNone;
    self.cellT.buttonEvaluation.alpha = 0;
    self.cellT.deleteOrder.alpha = 0;
    self.cellT.buttonContractWe.alpha =0;
    self.cellT.buttonSureGoods.alpha =1;
    self.cellT.cancelOrders.alpha = 0;
    self.cellT.buttonPayment.alpha =0;
    self.cellT.buttonLookExpress.alpha =1;
    self.cellT.buttonlookExpressTwo.alpha =0;
    [self.cellT.buttonProductsDetails addTarget:self action:@selector(productAction:) forControlEvents:UIControlEventTouchUpInside];
    return self.cellT;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 288*FitH;
}
#pragma mark - 点击进入购买商品详情页面
- (void)productAction:(UIButton *)button {
    
    self.model = [[GoodModel alloc]init];

    
         ViewOrderViewController *order = [[ ViewOrderViewController alloc]initWithNibName:@"ViewOrderViewController" bundle:nil];
    order.model = self.model;
        [self.navigationController pushViewController:order animated:YES];


    
    
}
- (void)ReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
